﻿using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using MRMDS.Module;
using MRMDS.Utils;
using HtmlHelper = MRMDS.Utils.HtmlHelper;

namespace Demo.Controllers
{
    [ApiController]
    [Route("FastView")]
    public class FastViewController : ControllerBase
    {
        private readonly GlobalManager global;

        public FastViewController()
        {
            global = GlobalManager.Instance;
        }

        [HttpGet("Actors")]
        public ContentResult ShowActors()
        {
            HtmlHelper helper = new HtmlHelper();
            helper.Html(true);
            helper.Body(true);
            helper.Lable("Current Actors");
            helper.Table(true);
            helper.SmartTR(new string[] { "index", "name", "type", "position", "speed", "rotation", "command", "path", "uuid" });
            int index = 1;
            foreach (var item in global.UUID2Actor)
            {
                if (item.Value == null)
                {
                    continue;
                }
                Actor actor = (item.Value as Actor)!;
                helper.TR(true);
                helper.TD(index);
                helper.TD(actor.Info?.Name ?? "empty");
                helper.TD(actor.Info?.Type ?? "empty");
                helper.TD(PositionToString(actor.State?.Position));
                helper.TD((actor.State?.Speed ?? 0) + "km/h");
                helper.TD(actor.State?.Rotation ?? 0);
                helper.TD(actor.Command?.Type ?? "None");
                helper.TD(PositionsToString(actor.Path?.PathPoints));
                helper.TD(item.Key);
                helper.TR(false);
                index++;
            }
            helper.Table(false);
            if (index == 1)
            {
                helper.Lable("Tip info:It seemed that there is no client connect in as Actor.");
            }
            else
            {
                helper.Lable("Tip info:This view will not update automatic,refresh manuly.");
            }
            helper.Body(false);
            helper.Html(false);

            return base.Content(helper.GetResult(), "text/html");

            string PositionToString(Position? position)
            {
                return (position == null) ? "unset" : string.Format("({0:F6},{1:F6})", position.Value.X, position.Value.Y);
            }

            string PositionsToString(Position[]? position)
            {
                if (position == null || position.Length == 0)
                {
                    return "Empty";
                }
                StringBuilder builder = new StringBuilder();
                builder.Append(PositionToString(position[0]));
                for (int i = 1; i < position.Length; i++)
                {
                    builder.Append("=>");
                    builder.Append(PositionToString(position[1]));
                }
                return builder.ToString();
            }
        }

        [HttpGet("Viewers")]
        public ContentResult ShowViewers()
        {
            HtmlHelper helper = new HtmlHelper();
            helper.Html(true);
            helper.Body(true);
            helper.Lable("Current Viewers");
            helper.Table(true);
            helper.SmartTR(new string[] { "index", "uuid", "sets" });
            int index = 1;
            foreach (var item in global.UUID2Viewer)
            {
                if (item.Value == null)
                {
                    continue;
                }
                Viewer viewer = item.Value;
                helper.TR(true);
                helper.TD(index);
                helper.TD(viewer.UUID ?? "empty");
                helper.TD(item.Key);
                helper.TD(CombineString(item.Value.WatchSet));
                helper.TR(false);
                index++;
            }
            helper.Table(false);
            if (index == 1)
            {
                helper.Lable("Tip info:It seemed that there is no client connect in as Viewer.");
            }
            else
            {
                helper.Lable("Tip info:This view will not update automatic,refresh manuly.");
            }
            helper.Body(false);
            helper.Html(false);

            return base.Content(helper.GetResult(), "text/html");

            string CombineString(string[] array)
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (var item in array)
                {
                    stringBuilder.Append(item);
                    stringBuilder.Append("&");
                }
                return stringBuilder.ToString();
            }
        }

    }
}